const multer = require("multer"); //导入multer中间件
const CONFIG = require("../../config")
const { v4: uuidv4 } = require("uuid");
const fs = require("fs");
let mydata = new Date();
let saveDay = mydata.getFullYear().toString() + (mydata.getMonth() + 1).toString() + mydata.getDate().toString();
let lengthName = 35;
module.exports = {
    //upload: upload,
    savefile(req, res) {
        //const upload = multer({ dest: __dirname + '/../../uploads/' + saveDay })

        let storage = multer.diskStorage({
            //设置上传后文件路径，uploads文件夹会自动创建。
            destination: function(req, file, cb) {
                cb(null, CONFIG.uploads + saveDay)
            },
            //给上传文件重命名，获取添加后缀名
            filename: function(req, file, cb) {
                var fileFormat = (file.originalname).split(".");
                cb(null, file.fieldname + '-' + uuidv4() + "." + fileFormat[fileFormat.length - 1]);
            }
        });
        //添加配置文件到muler对象。
        let upload = multer({
            storage: storage
        });
        //console.log("-------upload2--------", upload)
        return upload;
    },
    getBgNum(req, res, next) {
        //获取该文件夹内的bg个数
        console.log("req.````", req.query.isImg)
        let readDir;
        if (req.query.isImg == "ImgBg") {
            readDir = fs.readdirSync(__dirname + CONFIG.bg);
        } else if (req.query.isImg == "Image") {
            readDir = fs.readdirSync(__dirname + CONFIG.source);
        } else if (req.query.isImg == "Border") {
            readDir = fs.readdirSync(__dirname + CONFIG.border);
        }
        lengthName = readDir.length + 1;
        console.log("------lengthName-----", lengthName)
        next();
    },
    saveBackground(req, res, next) {
        let storages = multer.diskStorage({
            //设置上传后文件路径
            destination: function(req, file, cb) {
                cb(null, __dirname + CONFIG.bg)
            },
            //给上传文件重命名，获取添加后缀名
            filename: function(req, file, cb) {
                var fileFormat = (file.originalname).split(".");
                cb(null, "bg" + lengthName + "." + fileFormat[fileFormat.length - 1]);
            }
        });
        //添加配置文件到muler对象。
        let uploads = multer({
            storage: storages
        });
        //console.log("-------upload2--------", upload)
        return uploads;
    },
    saveImage() {
        let storages = multer.diskStorage({
            //设置上传后文件路径
            destination: function(req, file, cb) {
                cb(null, __dirname + CONFIG.source)
            },
            //给上传文件重命名，获取添加后缀名
            filename: function(req, file, cb) {
                var fileFormat = (file.originalname).split(".");
                cb(null, "source" + lengthName + "." + fileFormat[fileFormat.length - 1]);
            }
        });
        //添加配置文件到muler对象。
        let uploads = multer({
            storage: storages
        });
        //console.log("-------upload2--------", upload)
        return uploads;
    },
    saveBorder() {
        let storages = multer.diskStorage({
            //设置上传后文件路径
            destination: function(req, file, cb) {
                cb(null, __dirname + CONFIG.border)
            },
            //给上传文件重命名，获取添加后缀名
            filename: function(req, file, cb) {
                var fileFormat = (file.originalname).split(".");
                cb(null, "border" + lengthName + "." + fileFormat[fileFormat.length - 1]);
            }
        });
        //添加配置文件到muler对象。
        let uploads = multer({
            storage: storages
        });
        return uploads;
    }
}